<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/4
 * Time: 17:27
 */
namespace App\Modules\Jobs\Services;
use App\Modules\User\Models\UserAverageDetail;
use Illuminate\Support\Facades\Log;
use Redis;

class UpdateUserAverageService {

    /**
     * 更新用户打赏次数及打赏金额
     * @param $uid
     * @return mixed
     */
    public static function UpdateUserRewardsInfo($value){
        $rewardTimes = Redis::hget('average_task_info:','rewards_count');
        $rewardTotal = Redis::hget('average_task_info:','rewards_total_fee');
        if($rewardTimes && $rewardTotal) {
            $rewardTimes++;
            $rewardTotal+=$value;

        }else{
            $info = UserAverageDetail::getAllUsersRewardsInfo();
            $rewardTimes = $info['rewardTimes'];
            $rewardTotal = $info['rewardTotal'];
        }
        Redis::hset('average_task_info:','rewards_count',$rewardTimes);
        Redis::hset('average_task_info:','rewards_total_fee',$rewardTotal);
        return ['rewardTimes'=>$rewardTimes,'rewardTotal'=>$rewardTotal];
    }

    /**
     * 更新用户所有已发布的任务数
     * @param $uid
     * @return mixed
     */
    public static function getUserTaskNum()
    {
        $task_num = Redis::hget('average_task_info:', 'task_count');
        if ($task_num) {
            $task_num++;
        }else{
            $task_num = UserAverageDetail::getAllUserTaskNum();
        }
        Redis::hset('average_task_info:', 'task_count', $task_num);
        return $task_num;
    }

    /**
     * 更新用户感兴趣总数
     */
    public static function getUserPickTotal()
    {
        //$interest = 0;
        $interest = Redis::hget('average_task_info:', 'interest_count');
        if ($interest ) {
            $interest++;
        }else{
            $interest =UserAverageDetail::getAllUserPickTotal();
        }
        Redis::hset('average_task_info:', 'interest_count', $interest);
        return $interest;
    }

    /**
     * 更新用户处理总时长
     * @param $uid
     */
    public static function UpdateAllHandleDuration($value){
        $totalTime  = Redis::hget('average_task_info:', 'handle_totalTime');
        $totalCount = Redis::hget('average_task_info:', 'handle_totalCount');
        Log::info('totalTime3'.$totalTime);
        Log::info('totalCount3'.$totalCount);
        if ($totalTime && $totalCount) {
            $totalTime++;
            $totalCount += $value;
            Log::info('totalTime0'.$totalTime);
            Log::info('totalCount0'.$totalCount);
        }else{
            $info = UserAverageDetail::handleAllDuration();
            Log::info(var_export($info,true));
            $totalTime = $info['totalTime'];
            $totalCount = $info['totalCount'];
            Log::info('totalTime1'.$totalTime);
            Log::info('totalCount1'.$totalCount);
        }
        Redis::hset('average_task_info:','handle_totalTime',$totalTime);
        Redis::hset('average_task_info:','handle_totalCount',$totalCount);
        Log::info('totalTime4'.$totalTime);
        Log::info('totalCount4'.$totalCount);
        return ['totalTime'=>$totalTime,'totalCount'=>$totalCount];
    }

}